package cn.edu.cug.cs.gtl.plugins.device.dialog;

import cn.edu.cug.cs.gtl.apis.ui.devicetree.DeviceTreeNodeData;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StorageClusterDialog extends JDialog {
    private JTextField connectionNameField = new JTextField();
    private JTextField urlField = new JTextField();
    private JTextField userField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();

    // 表示用户是否点击了确认按钮
    private boolean isConfirmed = false;

    public StorageClusterDialog(JFrame parent) {
        super(parent, "存储集群配置", true);

        setLayout(new GridLayout(5, 2));
        add(new JLabel("ConnectionName:"));
        add(connectionNameField);
        add(new JLabel("URL:"));
        add(urlField);
        add(new JLabel("User:"));
        add(userField);
        add(new JLabel("Password:"));
        add(passwordField);

        JButton confirmButton = new JButton("Confirm");
        JButton cancelButton = new JButton("Cancel");

        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isConfirmed = true;
                setVisible(false);
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isConfirmed = false;
                setVisible(false);
            }
        });

        add(confirmButton);
        add(cancelButton);

        pack();
        setLocationRelativeTo(parent);
    }

    public String getConnectionName() {
        return connectionNameField.getText();
    }

    public String getUrl() {
        return urlField.getText();
    }

    public String getUser() {
        return userField.getText();
    }

    public String getPassword() {
        return String.valueOf(passwordField.getPassword());
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (!visible) {
            // Clear the password field when the dialog is closed
            // passwordField.setText(null);
        }
    }

    public boolean isConfirmed() {
        return isConfirmed;
    }

    public void setInitialValues(String connectionName, String url, String user, String password) {
        connectionNameField.setText(connectionName);
        urlField.setText(url);
        userField.setText(user);
        passwordField.setText(password);
    }


}
